<?php
define("IN_GBOOK",TRUE);
error_reporting(0);

include_once('./include/mysql.class.php');
include_once('./include/config.inc.php');
include_once('./view/index.html');
$db=new mysql($dbhost,$dbusername,$dbpassword,$dbname);
include_once('./include/gbook.class.php');
$result=new manage_gbook;
$act=strip_tags($_GET['act']);
if($act=='add'){
	$title=strip_tags($_POST['message']);
	$username=strip_tags($_POST['username']);
	$message=str_replace(array('\n',' '),array('<br>','&nbsp;'),$_POST['message']);
	$message=strip_tags($message);
	$time=time();
	if($_POST['gbsubmit']){
	$jieguo=$db->query("INSERT INTO message(`title`,`message`,`addtime`,`edittime`) VALUES ('$title','$message','$time','$time')");
	  if(empty($jieguo)){
			echo '���ʧ��';
			exit;
		}else{
			echo '��ӳɹ�';
			echo '<br><a href="index.php">������ҳ</a>';
			exit;			
		}
	}	
}elseif($act=='edit'){
	$id=intval($_GET['id']);
	if($_POST['editsubmit'] && $id){
		$title=strip_tags($_POST['title']);
		$username=strip_tags($_POST['username']);
		$message=str_replace(array('\n',' '),array('<br>','&nbsp;'),$_POST['message']);
		$message=strip_tags($message);
		$time=time();
		$sql="UPDATE message SET `title`='$title',`message`='$message',`username`='$username' WHERE id='$id'";
		if($result->edit($id,'editsubmit',$sql)){
			echo '�༭�ɹ�';
			exit;
		}		
	}else{
	  $info=$result->edit($id);
	  include('./edit.php');
	}	
}elseif($act=='del'){
	$id=intval($_GET['id']);
	if(!empty($id)){
		$result->del($id);
	}else{
		echo 'ID�Ƿ�';
	    echo '<br><a href="index.php">������ҳ</a>';	
		exit;
	}
	    echo 'ɾ��ɹ�';
	    echo '<br><a href="index.php">������ҳ</a>';
}else{	
	$shuju=$result->showmsg("SELECT * from message");
	$shumu=$db->query("SELECT COUNT(*) from message");
	$shumu=$db->result($shumu,0);
	echo $shumu;exit;
	$list='';
	foreach($shuju as $value){
		$list.='<ul><li>'.$value[title].'</li><li>'.$value[message].'</li><li>'.$value[username].'</li><li><a href="index.php?act=del&id='.$value[id].'" onclick="return confirm(\'���Ҫɾ����\');">ɾ��</a></li><li><a href="index.php?act=edit&id='.$value[id].'">�༭</a></li></ul>';
	}
	echo $list;
}
?>
